let tmpv2 = cc.v2();
export class Smoother {
    private _list: cc.Vec2[] = [];
    private _nextUpdateSlot: number = 0;
    private _maxSampleSize: number = 0;
    public constructor(sampleSize: number) {
        this._nextUpdateSlot = 0;
        this._maxSampleSize = sampleSize;
    }

    public onUpdate(recentValue: cc.Vec2):cc.Vec2{
        let v = this._list[this._nextUpdateSlot];
        if (!v) {
            v = cc.v2(recentValue);
            this._list[this._nextUpdateSlot] = v;
        } else {
            v.set(recentValue);
        }
        this._nextUpdateSlot++;
        if (this._nextUpdateSlot >= this._maxSampleSize) this._nextUpdateSlot = 0;
        tmpv2.set(cc.Vec2.ZERO);
        for (let i = 0, len = this._list.length; i < len; i++) {
            cc.Vec2.add(tmpv2, this._list[i], tmpv2);
        }
        tmpv2.divSelf(this._list.length);
        return tmpv2;
    }
}